package com.haois.mp.database.module.offaccounts.controller;


import com.google.gson.Gson;
import com.haois.mp.database.commom.enums.MpTypeEnum;
import com.haois.mp.database.exception.ErrorException;
import com.haois.mp.database.exception.OutException;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import com.haois.mp.database.module.basic.controller.BaseController;
import com.haois.mp.database.module.remote.service.IUserService;
import com.haois.mp.database.module.common.service.IUserTokenService;
import com.haois.mp.database.util.mp.DataCheck;
import me.chanjar.weixin.common.exception.WxErrorException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
@RequestMapping("/wxmp/user")
public class UserController extends BaseController {

    private static final Logger logger = LoggerFactory
            .getLogger(UserController.class);

    @Autowired
    private IUserService iUserService;

    @Autowired
    private IUserTokenService iUserTokenService;

    private Gson gson = new Gson();

    /**
     * 个人信息
     *
     * @param request
     * @return
     * @throws RuntimeException
     * @throws WxErrorException
     */
    @RequestMapping("/info")
    public ModelAndView info(HttpServletRequest request) throws RuntimeException, WxErrorException, ErrorException, OutException {

        ModelAndView mv = new ModelAndView("/user/info");
        String access_token = (String) request.getAttribute("access_token");
        Map result = iUserService.getUserInfo(access_token);
        String openId = wxOAuth2AccessTokenUtil.getOpenid(request);
        WxMpUser wxMpUser = wxMpService.getUserService().userInfo(openId);
        logger.info("用户信息为:[{}]", gson.toJson(wxMpUser));
        DataCheck.codeIs16(result);
        mv.addObject("wxMpUser", gson.toJson(wxMpUser));
        mv.addObject("result", gson.toJson(result));
        return mv;
    }


    @RequestMapping("/hisInfo")
    public ModelAndView hisInfo(HttpServletRequest request, String oid, String memberNum)
            throws RuntimeException, WxErrorException, ErrorException {
        ModelAndView mv = new ModelAndView("/user/hisInfo");
        mv.addObject("fromUser", DataCheck.getOpenId(request));
        mv.addObject("type", "hisInfo");
        mv.addObject("oid", oid);
        mv.addObject("memberNum", memberNum);
        return mv;
    }

    @RequestMapping("/toDetail")
    public ModelAndView detail(HttpServletRequest request, String type)
            throws RuntimeException, WxErrorException, ErrorException {
        String fromPage = request.getParameter("fromPage");
        ModelAndView mv = new ModelAndView("/user/detail");
        mv.addObject("fromPage", fromPage);
        mv.addObject("type", type);
        return mv;
    }

    /**
     * 跳转到注册界面
     */
    @RequestMapping("/toRegisterPage")
    public ModelAndView toRegisterPage() throws RuntimeException {
        ModelAndView mv = new ModelAndView("/user/register");
        return mv;
    }


    /**
     * 用户登录
     *
     * @param url     用户登录前请求的url
     * @param request
     * @return
     */
    @RequestMapping(value = "/login")
    public ModelAndView login(String url, HttpServletRequest request) throws RuntimeException {
        ModelAndView mv = new ModelAndView("/user/login");

        mv.addObject("url", url);
        return mv;
    }

    /**
     * 用户登录
     *
     * @param url     用户登录前请求的url
     * @param request
     * @return
     */
    @RequestMapping(value = "/complete")
    public ModelAndView complete(String url, HttpServletRequest request) throws RuntimeException {
        String memberNum = request.getParameter("memberNum");
        ModelAndView mv = new ModelAndView("/user/complete");
        mv.addObject("memberNum", memberNum);
        return mv;
    }

    /**
     * 跳转到修改密码界面
     */
    @RequestMapping("/toUpdatePSW")
    public ModelAndView toUpdatePSWPage(HttpServletRequest request)
            throws RuntimeException, OutException, ErrorException {
        String access_token = (String) request.getAttribute("access_token");

        Map result = iUserService.getUserInfo(access_token);
        DataCheck.codeNot0X(result);
        Map userInfo = (Map) result.get("data");

        ModelAndView mv = new ModelAndView("/user/updatePSW");
        mv.addObject("userInfo", userInfo);
        return mv;
    }

    /**
     * 跳转到忘记密码页面
     *
     * @return
     */
    @RequestMapping("/toResetPSWPage")
    public ModelAndView toForget() {
        ModelAndView mv = new ModelAndView("/user/forgetPSW");
        return mv;
    }

    /**
     * /**
     * 退出登录
     */
    @RequestMapping("/logout")
    public ModelAndView logout(
            HttpServletRequest request)
            throws WxErrorException {
        ModelAndView mv = new ModelAndView("redirect:/wxmp/user/login");
        String openid = wxOAuth2AccessTokenUtil.getOpenid(request);
        logger.info("openId为[{}]的用户退出登录", openid);
        if (openid != null && StringUtils.isNotEmpty(openid)) {
            iUserTokenService.logout(openid, MpTypeEnum.OFFACCOUNTS.getType(), "退出登录");
        }
        return mv;
    }


}
